package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TImonthcontent;
import com.zmht.specedu.vo.Teacher;

public class ImonthcontentAction extends BaseAction {
	/**
	 * 保存月内容（同时将月主题名称保存）
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveIMonthcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String month = request.getParameter("month");
		String monthThemeName = request.getParameter("monthContent");
		String content = request.getParameter("imonthContent");
		
		TImonthcontent monthcontent = new TImonthcontent();
		monthcontent.setMonth(new Integer(month));
		monthcontent.setMonthThemeName(monthThemeName);
		monthcontent.setMonthcontent(content);
		monthcontent.setTeacherId(teacher.getTeacherId());
		//monthcontent.setCtime(ctime)
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			monthcontent.setIaimId(new Integer(targetId));
		}
		
		this.getImonthcontentBiz().saveTImonthcontent(monthcontent);
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			return new ActionForward("/beike/imonthcontent.do?method=getIMonthcontentEdit&month="+month+"&targetId="+targetId);
		}
		return new ActionForward("/beike/imonthcontent.do?method=getIMonthcontent&month="+month);
	}
	/**
	 * 获得月主题内容（同时获得月主题名称）
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIMonthcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String month = request.getParameter("month");
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		// 开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		List<TImonthcontent> list = this.getImonthcontentBiz().getTImonthcontent(teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);//this.getImonthinfoBiz().getImonthinfo(teacher.getTeacherId(), new Integer(month), (index-1)*pageSize, pageSize);
		int total = this.getImonthcontentBiz().getTImonthcontentTotal(teacher.getTeacherId(), month);
		
		String monthContent = "";
		if(list!=null&&list.size()>0){monthContent = list.get(0).getMonthThemeName();}
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("month", month);
		
		request.setAttribute("monthcontents", list);
		request.setAttribute("monthContent", monthContent);
		return mapping.findForward("newjiaoxue_content");
	}
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delIMonthcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String month = request.getParameter("month");
		this.getImonthcontentBiz().delTImonthcontent(new Integer(id));
		
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			return new ActionForward("/beike/imonthcontent.do?method=getIMonthcontentEdit&month="+month+"&targetId="+targetId);
		}
		return new ActionForward("/beike/imonthcontent.do?method=getIMonthcontent&month="+month);
	}
	/**
	 * 更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateMonthcontent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("cid");
		String month = request.getParameter("month");
		String content = request.getParameter("imonthContent");
		
		TImonthcontent monthcontent = this.getImonthcontentBiz().findById(new Integer(id));
		if(null!=monthcontent){
			monthcontent.setMonthcontent(content);
			this.getImonthcontentBiz().updateImonthcontent(monthcontent);
		}
		//this.getImonthcontentBiz().delTImonthcontent(new Integer(id));
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			return new ActionForward("/beike/imonthcontent.do?method=getIMonthcontentEdit&month="+month+"&targetId="+targetId);
		}
		
		return new ActionForward("/beike/imonthcontent.do?method=getIMonthcontent&month="+month);
	}
	/**
	 * 用于编辑目标时 获得月主题内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIMonthcontentEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String month = request.getParameter("month");
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String targetId = request.getParameter("targetId");
		
		// 开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		//List<TImonthcontent> list = this.getImonthcontentBiz().getTImonthcontent(teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);//this.getImonthinfoBiz().getImonthinfo(teacher.getTeacherId(), new Integer(month), (index-1)*pageSize, pageSize);
		//int total = this.getImonthcontentBiz().getTImonthcontentTotal(teacher.getTeacherId(), month);
		List<TImonthcontent> list = this.getImonthcontentBiz().getTImonthcontentEdit(new Integer(targetId), teacher.getTeacherId(), month, (index-1)*pageSize, pageSize);
		int total = this.getImonthcontentBiz().getTImonthcontentEditTotal(new Integer(targetId), teacher.getTeacherId(), month);
		
		String monthContent = "";
		if(list!=null&&list.size()>0){monthContent = list.get(0).getMonthThemeName();}
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("month", month);
		
		request.setAttribute("monthcontents", list);
		request.setAttribute("monthContent", monthContent);
		
		request.setAttribute("targetId", targetId);
		request.setAttribute("edit", "1");
		
		return mapping.findForward("newjiaoxue_content");
	}
}
